<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * 市场动态
 * 
 * @author  : $Author: spike $
 * @date    : $Date: 2014-08-06 17:05:36 +0800 (三, 2014-08-06) $
 * @version : $Id: trend.php 126 2014-08-06 09:05:36Z spike $
 * @rev     : $Revision: 126 $
 */
class Trend extends CI_Controller 
{  
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * [浏览]
     */
    public function browse()
    {
        $params = $this->input->get();
        if($params)
        {
            $this->load->model('Trend_model');
            $list = $this->Trend_model->browse_trend($params, $params['page'], $params['size']);

            $this->load->library('pagination');
            $config['total_rows'] = $list['total'];
            $config['per_page']   = $list['size'];
            $config['suffix']     = '#data-table';

            $this->pagination->initialize($config); 
            $pagination = $this->pagination->create_links();
        }

        // 统计
        $this->load->dao('Trend_dao');
        $total = $this->Trend_dao->count(array(
            'is_delete' => UNDELETED
        ));

        $data = array(
            'default'      => $params ? $params : array(),
            'total'        => $total,
            'list'         => $list,
            'pagination'   => $pagination,
            'pagesize'     => $this->config->item('pagesize'),
            'trend_status' => $this->config->item('trend_status'),
        );

        $this->load->view('home/header.tpl', $data);
        $this->load->view('trend/browse.tpl');
        $this->load->view('home/footer.tpl');
    }

    /**
     * [新增]
     */
    public function add()
    {
        if($this->input->is_post_request())
        {
            $params = $this->input->post();

            temp('add_trend', $params);

            $this->load->model('Trend_model');

            $config = array(
                'upload_path'   => FRONTEND_DATA_PATH.'trend',
                'allowed_types' => 'jpg|jpeg|png|gif|bmp',
                'encrypt_name'  => TRUE,
                'overwrite'     => FALSE,
            );
      
            $this->load->library('upload', $config);
            if( ! $this->upload->do_upload('img'))
            {
                show_error($this->upload->display_errors());
            }
            $upload = $this->upload->data();
            $params['cover_img'] = $upload['file_name'];

            if( FALSE === $trend_id = $this->Trend_model->add_trend($params) )
            {
                show_error($this->Trend_model->get_error(), 200, '错误', 'error_general_lite');
            }

            temp('add_trend', NULL);

            show_message(
                "新增市场动态成功！", 
                array(
                    '继续编辑商品'   => DOMAIN."trend/add",
                    '查看新建的商品' => DOMAIN."trend/edit/{$trend_id}",
                ),
                200,
                '系统提示',
                'message_general_lite'
            );
        }
        else
        {
            $page_style    = $this->config->item('page_style');
            $data = array(
                'temp'         => temp('add_trend'),
                'page_style'   => $page_style['LITE'],
                'trend_status' => $this->config->item('trend_status'), 
                'js'           => array(
                    'jquery/jquery-ueditor-mini/umeditor.config.js',
                    'jquery/jquery-ueditor-mini/umeditor.js',
                ),
                'css'        => array(JS_DOMAIN.'jquery/jquery-ueditor-mini/themes/default/css/umeditor.css'),
            );
            $this->load->view('home/header.tpl', $data);
            $this->load->view('trend/add.tpl');
            $this->load->view('home/footer.tpl');
        }
    }

    /**
     * [编辑]
     */
    public function edit($sn = NULL)
    {
        if($this->input->is_post_request())
        {
            $params = $this->input->post();

            if( $_FILES['img']['name'] )
            {
                $config = array(
                    'upload_path'   => FRONTEND_DATA_PATH.'trend',
                    'allowed_types' => 'jpg|jpeg|png|gif|bmp',
                    'encrypt_name'  => TRUE,
                    'overwrite'     => FALSE,
                );
          
                $this->load->library('upload', $config);
                if( ! $this->upload->do_upload('img'))
                {
                    show_error($this->upload->display_errors());
                }
                $upload = $this->upload->data();
                $params['cover_img'] = $upload['file_name'];
            }

            $this->load->model('Trend_model');

            if( FALSE === $this->Trend_model->edit_trend($sn, $params) )
            {
                show_error($this->Trend_model->get_error(), 200, '错误', 'error_general_lite');
            }

            show_message(
                "编辑市场动态成功！", 
                array('查看编辑的市场动态' => DOMAIN."trend/edit/{$sn}",),
                200,
                '系统提示',
                'message_general_lite'
            );
        }
        else
        {
            $this->load->model('Trend_model');
            if( ! $trend = $this->Trend_model->get_trend($sn, FALSE))
            {
                show_error($this->Trend_model->get_error(), 200, '错误', 'error_general_lite');
            }

            $page_style    = $this->config->item('page_style');
            $data = array(
                'trend'        => $trend,
                'page_style'   => $page_style['LITE'],
                'trend_status' => $this->config->item('trend_status'),
                'js'           => array(
                    'jquery/jquery-ueditor-mini/umeditor.config.js',
                    'jquery/jquery-ueditor-mini/umeditor.js',
                ),
                'css'        => array(JS_DOMAIN.'jquery/jquery-ueditor-mini/themes/default/css/umeditor.css'),
            );
            $this->load->view('home/header.tpl', $data);
            $this->load->view('trend/edit.tpl');
            $this->load->view('home/footer.tpl');
        }
    }

    /**
     * [改变状态]
     */
    public function change_status($sn = NULL)
    {
        $act     = $this->input->get('act');
        $is_ajax = $this->input->get('is_ajax');

        $this->load->model('Trend_model');
        if( FALSE === $this->Trend_model->change_trend_status($sn, $act))
        {
            if($is_ajax === '1')
            {
                json_exit($this->Trend_model->get_error());
            }
        }
        else
        {
            if($is_ajax === '1')
            {
                json_exit('操作成功', TRUE);
            }
        }
    }

}
/* End of file */